package com.zigong.app.beans;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.ruihang.generalibrary.ui.util.IListData;
import com.zigong.app.R;
import com.zigong.app.application.Constances;
import com.zigong.app.ui.datainf.IHour24Data;
import com.zigong.app.ui.util.IListShowData;

import java.io.Serializable;
import java.sql.Date;
import java.util.ArrayList;


/**
 * Created by yuxipan on 2017-11-14.
 */

public class TopicItem implements IHour24Data, Serializable {
    protected String channelName;
    protected boolean isLiving;
    protected String newsId;
    protected String newsImage;
    protected String newsMediaLength;
    protected String newsTitle;
    protected int newsType;
    protected String newsUrl;
    protected String pubTime;
    protected String skipType;
    protected String newsKeyword;
    protected String keyword;
    protected int newsImageById;
    /**
     * 是否为焦点新闻
     */
    protected boolean isFocusNews;

    protected String lable;

    protected IListShowData owner;
    protected int viewType;
    protected int itemMode;
    private ArrayList<BannerFocusItem> banners;
    private ArrayList<LiveFocusItem> lives;
    /**
     * 是否是跟在title后面的
     */
    private int afterTitleMode = -1;

    protected Date pubTimeTemp;
    protected boolean lastItem;

    public ArrayList<LiveFocusItem> getLives() {
        return lives;
    }

    public void setLives(ArrayList<LiveFocusItem> lives) {
        this.lives = lives;
    }

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    @Override
    public String getDataTitle() {
        return newsTitle;
    }

    @Override
    public String getDataImg() {
        return newsImage;
    }

    @Override
    public String getDataTag() {
        return lable;
    }

    @Override
    public int getDataType() {
        return newsType;
    }

    @Override
    public int getDataViewType() {
        return viewType;
    }

    @Override
    public IListData getOwner() {
        return owner;
    }

    @Override
    public String getDataMediaLength() {
        return newsMediaLength;
    }

    @Override
    public boolean shouldShowMedia() {
        return false;
    }

    @Override
    public int getItemMode() {
        return itemMode;
    }

    @Override
    public boolean isLiving() {
        return isLiving;
    }

    @Override
    public String getDataId() {
        return newsId;
    }

    @Override
    public String getTime() {
        return pubTime;
    }

    @Override
    public IListShowData getOwenerData() {
        return owner;
    }

    public String getChannelName() {
        return channelName;
    }

    public void setChannelName(String channelName) {
        this.channelName = channelName;
    }

    public boolean isIsLiving() {
        return isLiving;
    }

    public void setIsLiving(boolean isLiving) {
        this.isLiving = isLiving;
    }

    public String getNewsId() {
        return newsId;
    }

    public void setNewsId(String newsId) {
        this.newsId = newsId;
    }

    public String getNewsImage() {
        return newsImage;
    }

    public void setNewsImage(String newsImage) {
        this.newsImage = newsImage;
    }

    public String getNewsMediaLength() {
        return newsMediaLength;
    }

    public void setNewsMediaLength(String newsMediaLength) {
        this.newsMediaLength = newsMediaLength;
    }

    public String getNewsTitle() {
        return newsTitle;
    }

    public void setNewsTitle(String newsTitle) {
        this.newsTitle = newsTitle;
    }

    public int getNewsType() {
        return newsType;
    }

    public void setNewsType(int newsType) {
        this.newsType = newsType;
    }

    public String getNewsUrl() {
        return newsUrl;
    }

    public void setNewsUrl(String newsUrl) {
        this.newsUrl = newsUrl;
    }

    public String getPubTime() {
        return pubTime;
    }

    public void setPubTime(String pubTime) {
        if (!TextUtils.equals(pubTime, this.pubTime)) {
            pubTimeTemp = null;
        }
        this.pubTime = pubTime;
    }

    public int getNewsImageById() {
        return newsImageById;
    }

    public void setNewsImageById(int newsImageById) {
        this.newsImageById = newsImageById;
    }

    public int getAfterTitleMode() {
        return afterTitleMode;
    }

    public void setAfterTitleMode(int afterTitleMode) {
        this.afterTitleMode = afterTitleMode;
    }

    public String getSkipType() {
        return skipType;
    }

    public void setSkipType(String skipType) {
        this.skipType = skipType;
    }

    public String getLable() {
        return lable;
    }

    public void setLable(String lable) {
        this.lable = lable;
    }

    public void setOwner(IListShowData owner) {
        this.owner = owner;
    }

    public void setViewType(int viewType) {
        this.viewType = viewType;
    }

    public void setItemMode(int itemMode) {
        this.itemMode = itemMode;
    }

    public ArrayList<BannerFocusItem> getBanners() {
        return banners;
    }

    public void setBanners(ArrayList<BannerFocusItem> banners) {
        this.banners = banners;
    }

    public boolean isFocusNews() {
        return isFocusNews;
    }

    public void setFocusNews(boolean focusNews) {
        isFocusNews = focusNews;
    }

    public TopicItem setIM(int itemMode) {
        setItemMode(itemMode);
        return this;
    }

    public String getNewsKeyword() {
        return newsKeyword;
    }

    public TopicItem setNewsKeyword(String newsKeyword) {
        this.newsKeyword = newsKeyword;
        return this;
    }

    public boolean isLastItem() {
        return lastItem;
    }

    public void setLastItem(boolean lastItem) {
        this.lastItem = lastItem;
    }

    @Override
    public String toString() {
        return getToStringHead() +
                Integer.toHexString(hashCode()) +
                " @ channelName='" + channelName + '\'' +
                ", viewType=" + viewType +
                ", isLiving=" + isLiving +
                ", lable=" + lable +
                ", newsId='" + newsId + '\'' +
                ", newsImage='" + newsImage + '\'' +
                ", newsMediaLength='" + newsMediaLength + '\'' +
                ", newsTitle='" + newsTitle + '\'' +
                ", newsType=" + newsType +
                ", newsUrl='" + newsUrl + '\'' +
                ", isFocusNews='" + isFocusNews + '\'' +
                ", pubTime='" + pubTime + '\'' +
                ", skipType=" + skipType +
                ", newsKeyword='" + newsKeyword + '\'' +
                ", owner=" + owner + getChildToString() +
                '}';
    }

    @NonNull
    protected String getToStringHead() {
        return "NewsItem{";
    }

    @NonNull
    protected String getChildToString() {
        return "";
    }

    /**
     * 是否相同，只检查id、title和newsUrl
     *
     * @return
     */
    public boolean isSigledSame(NewsItem item) {
        if (item == null) {
            return false;
        }
        return TextUtils.equals(item.getNewsId(), this.newsId)
                && TextUtils.equals(item.getNewsTitle(), this.newsTitle)
                && TextUtils.equals(item.getNewsUrl(), this.newsUrl);
    }

    public static NewsItem createTestInstance(int i, int viewType) {
        NewsItem newsItem = new NewsItem();
        newsItem.setViewType(viewType);
        newsItem.setNewsTitle("首都机场一厦航飞机逃生滑梯疑遭乘客误放");
        newsItem.setPubTime("ss");
        newsItem.setChannelName("测试");
        newsItem.setLable("测试");
        if (i < 0) {
            newsItem.pubTimeTemp = new Date(System.currentTimeMillis());
        } else {
            newsItem.pubTimeTemp = new Date((long) (System.currentTimeMillis() - i - Math.random() * 600));
        }
        if (i == 0) {
            newsItem.setItemMode(1);
        } else if (i == -2) {
            newsItem.setItemMode(2);
        }
        int mode = (int) (Math.random() * 4);
        if (mode == NEWSMODE_LIVE) {
            newsItem.setIsLiving(true);
            mode = 0;
        }
        newsItem.setNewsType(mode);
        if (mode > 0) {
            String len = null;
            mode = (int) (Math.random() * 60);
            boolean b = mode == 0;
            if (mode < 10) {
                len = "0" + mode + ":";
            } else {
                len = mode + ":";
            }
            mode = (int) (Math.random() * 60);
            if (mode < 10) {
                if (b) {
                    mode = (int) (10 + Math.random() * 50);
                    len = len + mode + "";
                } else {
                    len = len + "0" + mode;
                }
            } else {
                len = len + mode + "";
            }

            if (mode == 0) {
                mode = (int) (30 + Math.random() * 1000);
            }

            newsItem.setNewsMediaLength(len);
        } else {

        }
        return newsItem;
    }

    @Override
    public Date getDataDate() {
        return pubTimeTemp;
    }

    @Override
    public void setDataDate(Date date) {
        this.pubTimeTemp = date;
    }

    @NonNull
    @Override
    public String getDatePattern() {
        return "yyyy-MM-dd HH:mm:ss";
    }

    @Override
    public String getDateString() {
        return pubTime;
    }

    public static int getTypeStrRes(int newsType) {
        switch (newsType) {
            default:
                return R.string.news_type_default;
            case Constances.NEWS_TYPE_GRAPHIC:
                return R.string.news_type_graphic;
            case Constances.NEWS_TYPE_AUDIO:
                return R.string.news_type_audio;
            case Constances.NEWS_TYPE_VIDEO:
                return R.string.news_type_video;
            case Constances.NEWS_TYPE_TOPIC:
                return R.string.news_type_topic;
            case Constances.NEWS_TYPE_LIVE:
                return R.string.news_type_live;
        }
    }

    public boolean isNewsTypeValued() {
        return newsType > 0 && newsType <= 6;
    }

}
